package com.msmall.community.space.action.html;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.msmall.base.action.HTMLBaseAction;
import com.msmall.base.pager.PaginationSupport;
import com.msmall.community.album.model.Album;
import com.msmall.community.album.service.AlbumManager;
import com.msmall.community.bbs.model.Favorite;
import com.msmall.community.bbs.service.FavoriteManager;
import com.msmall.community.common.model.Message;
import com.msmall.community.common.service.MessageManager;
import com.msmall.community.diary.service.DiaryManager;
import com.msmall.community.mood.service.MoodManager;
import com.msmall.community.play.service.PlayManager;
import com.msmall.community.roster.model.RosterGroup;
import com.msmall.community.roster.service.RosterManager;
import com.msmall.community.space.model.Words;
import com.msmall.community.space.service.WordsManager;
import com.msmall.community.space.util.SpaceConstants;
import com.msmall.community.team.model.Team;
import com.msmall.community.team.service.TeamManager;
import com.msmall.community.user.service.UserManager;

public class IndexAction extends HTMLBaseAction{
	private static final Log logger = LogFactory.getLog(IndexAction.class);
	private static final long serialVersionUID = 3755734169515435256L;

	private int part; //查看用户信息的哪个部分
	
	private PaginationSupport plays; //用户玩的插件
	
	private List<Team> teamList;//用户加入的群组
	
	private List<Favorite> favTopicList; // 用户所有收藏的帖子
	
	private List<Words> wordsList; // 用户所有的留言
	
	private List<Message> messageList; //用户所有的系统消息
	
	private List<RosterGroup> rosterGroups; //好友组
	
	private List<Album> albumList;//相册列表
	
	private UserManager userManager;
	
	private FavoriteManager favoriteManager;
	
	private WordsManager wordsManager;
	
	private PlayManager playManager;
	
	private MessageManager messageManager;
	
	private TeamManager teamManager;
	
	private RosterManager rosterManager;
	
	private AlbumManager albumManager;
	
	private DiaryManager diaryManager;
	
	private MoodManager moodManager;
	
	public String execute(){
		try{
			if(usr == null || usr == 0){
				if(!getIsLogin()){
					return ERROR;
				}else{
					usr = getUserid(); // 如果是登陆用户，跳转到自己的首页
				}
			}
			if(usr.compareTo(getUserid()) == 0){
				isOwner = true;
			}else{
				isOwner = false;
				userManager.addVisitAmount(usr);
			}
			userInfo = userManager.getByUserid(usr);
			userProfile = userManager.getProfileByUserid(userid);
		}catch(Exception e){
			logger.error(e);
		}
		return SUCCESS;
	}

	/**
	 * 查看用户信息的不同部分
	 */
	public String widget(){
		try{
			if(usr == null || usr == 0){
				if(!getIsLogin()){
					return ERROR;
				}else{
					usr = getUserid(); // 如果是登陆用户，跳转到自己的首页
				}
			}
			if(usr.compareTo(getUserid()) == 0){
				isOwner = true;
			}
			userInfo = userManager.getByUserid(usr);
			userProfile = userManager.getProfileByUserid(usr);
			switch(part){
				case SpaceConstants.SPACE_WIDGET_PART_HOME :{
					return widgetHome();
				}
				case SpaceConstants.SPACE_WIDGET_PART_DIARY :{
					return widgetDiary();
				}
				case SpaceConstants.SPACE_WIDGET_PART_FOOTERPRINT :{
					return widgetFooterprint();
				}
				case SpaceConstants.SPACE_WIDGET_PART_ALBUM :{
					return widgetAlbum();
				}
				case SpaceConstants.SPACE_WIDGET_PART_ROSTER :{
					return widgetRoster();
				}
				case SpaceConstants.SPACE_WIDGET_PART_WORDS :{
					return widgetWords();
				}
				case SpaceConstants.SPACE_WIDGET_PART_PROFILE :{
					return widgetProfile();
				}
				case SpaceConstants.SPACE_WIDGET_PART_MESSAGE :{
					return widgetMessage();
				}
				case SpaceConstants.SPACE_WIDGET_PART_TEAM :{
					return widgetTeam();
				}
				default : {
					return widgetHome();
				}
			}
		}catch(Exception e){
			log.error(e);
			return widgetHome();
		}
	}
	
	public String widgetHome(){
		try{
			page.setPageSize(10);
			if(isOwner){
				moodManager.getFriendMood(page, usr);
				return "homeOwner";
			}else{
				moodManager.getPagination(page, usr);
				return "homeViewer";
			}
		}catch(Exception e){
			logger.error(e);
			return ERROR;
		}
	}
	public String widgetDiary() throws Exception{
		diaryManager.diaryList(page, usr);
		if(isOwner){
			return "diaryOwner";
		}else{
			return "diaryViewer";
		}
	}
	public String widgetFooterprint(){
		if(isOwner){
			return "footerprintOwner";
		}else{
			return "footerprintViewer";
		}
	}
	public String widgetAlbum() throws Exception{
		albumList = albumManager.getAlumsByUserid(usr);
		albumList = albumList==null?new ArrayList<Album>():albumList;
		if(isOwner){
			return "albumOwner";
		}else{
			return "albumViewer";
		}
	}
	public String widgetRoster(){
		rosterGroups = rosterManager.getRosterGroupSelf(usr);
		rosterGroups = rosterGroups==null?new ArrayList<RosterGroup>():rosterGroups;
		if(isOwner){
			return "rosterOwner";
		}else{
			return "rosterViewer";
		}
	}
	public String widgetWords(){
		wordsList = wordsManager.getWordsByUserid(usr);
		wordsList = wordsList== null?new ArrayList<Words>():wordsList;
		if(isOwner){
			return "wordsOwner";
		}else{
			return "wordsViewer";
		}
	}
	public String widgetProfile(){
		if(isOwner){
			return "profileOwner";
		}else{
			return "profileViewer";
		}
	}
	public String widgetMessage(){
		messageList = messageManager.getMessagesByUserid(usr);
		messageList = messageList== null?new ArrayList<Message>():messageList;
		if(isOwner){
			return "messageOwner";
		}else{
			return ERROR;
		}
	}
	public String widgetTeam(){
		if(isOwner){
			return "profileOwner";
		}else{
			return "profileViewer";
		}
	}
	/**
	 * 查看用户信息的不同部分
	 */
	public String widget1(){
		
		if(usr == null || usr == 0){
			if(!getIsLogin()){
				return ERROR;
			}else{
				usr = getUserid(); // 如果是登陆用户，跳转到自己的首页
			}
		}
		if(usr.compareTo(getUserid()) == 0){
			switch(part){
				case 100:{	//	收藏的帖子
					favTopicList = favoriteManager.getAllByUserid(usr);
					favTopicList = favTopicList==null?new ArrayList<Favorite>():favTopicList;
					return "p_topic";
				}
				case 101:{//	好友
					rosterGroups = rosterManager.getRosterGroupSelf(usr);
					rosterGroups = rosterGroups==null?new ArrayList<RosterGroup>():rosterGroups;
					return "p_roster";		
				}
				case 102:{//	群组
					try {
						teamList = teamManager.loadTeamsByUserStatus(usr, Team.STATUS_NORMAL);
						if(teamList != null){
							teamList.addAll(teamManager.loadTeamsByUserStatus(usr, Team.STATUS_TOBE_AUDIT));
						}else{
							teamList = teamManager.loadTeamsByUserStatus(usr, Team.STATUS_TOBE_AUDIT);
						}
					} catch (Exception e1) {
					}
					teamList = teamList==null?new ArrayList<Team>():teamList;
					return "p_team";
				}
				case 103:{//	留言本
					wordsList = wordsManager.getWordsByUserid(usr);
					wordsList = wordsList== null?new ArrayList<Words>():wordsList;
					return "p_word";
				}
				case 104:{//	系统消息
					messageList = messageManager.getMessagesByUserid(usr);
					messageList = messageList== null?new ArrayList<Message>():messageList;
					return "p_message";
				}
				default:{
					return ERROR;
				}
			}
		}else{
			switch(part){
			case 100:{	//	收藏的帖子
				favTopicList = favoriteManager.getAllByUserid(usr);
				favTopicList = favTopicList==null?new ArrayList<Favorite>():favTopicList;
				return "p_topic_v";
			}
			case 101:{//	好友
				rosterGroups = rosterManager.getRosterGroupNormal(usr);
				rosterGroups = rosterGroups==null?new ArrayList<RosterGroup>():rosterGroups;
				return "p_roster_v";		
			}
			case 102:{//	群组
				try {
					teamList = teamManager.loadTeamsByUserStatus(usr, Team.STATUS_NORMAL);
					if(teamList != null){
						teamList.addAll(teamManager.loadTeamsByUserStatus(usr, Team.STATUS_TOBE_AUDIT));
					}else{
						teamList = teamManager.loadTeamsByUserStatus(usr, Team.STATUS_TOBE_AUDIT);
					}
				} catch (Exception e1) {
				}
				teamList = teamList==null?new ArrayList<Team>():teamList;
				return "p_team_v";
			}
			case 103:{//	留言本
				wordsList = wordsManager.getWordsByUserid(usr);
				wordsList = wordsList== null?new ArrayList<Words>():wordsList;
				return "p_word_v";
			}
			default:{
				return ERROR;
			}
		}
		}
	}
	public int getPart() {
		return part;
	}

	public void setPart(int part) {
		this.part = part;
	}

	public void setFavoriteManager(FavoriteManager favoriteManager) {
		this.favoriteManager = favoriteManager;
	}

	public PaginationSupport getPlays() {
		return plays;
	}

	public void setPlays(PaginationSupport plays) {
		this.plays = plays;
	}

	public List<Team> getTeamList() {
		return teamList;
	}

	public void setTeamList(List<Team> teamList) {
		this.teamList = teamList;
	}

	public List<Album> getAlbumList() {
		return albumList;
	}

	public void setAlbumList(List<Album> albumList) {
		this.albumList = albumList;
	}

	public List<Message> getMessageList() {
		return messageList;
	}

	public void setMessageList(List<Message> messageList) {
		this.messageList = messageList;
	}

	public List<Words> getWordsList() {
		return wordsList;
	}

	public void setWordsList(List<Words> wordsList) {
		this.wordsList = wordsList;
	}

	public List<Favorite> getFavTopicList() {
		return favTopicList;
	}

	public void setFavTopicList(List<Favorite> favTopicList) {
		this.favTopicList = favTopicList;
	}

	public List<RosterGroup> getRosterGroups() {
		return rosterGroups;
	}

	public void setRosterGroups(List<RosterGroup> rosterGroups) {
		this.rosterGroups = rosterGroups;
	}

	public void setPlayManager(PlayManager playManager) {
		this.playManager = playManager;
	}

	public void setWordsManager(WordsManager wordsManager) {
		this.wordsManager = wordsManager;
	}

	public void setMessageManager(MessageManager messageManager) {
		this.messageManager = messageManager;
	}

	public void setTeamManager(TeamManager teamManager) {
		this.teamManager = teamManager;
	}
	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	public void setRosterManager(RosterManager rosterManager) {
		this.rosterManager = rosterManager;
	}

	public void setAlbumManager(AlbumManager albumManager) {
		this.albumManager = albumManager;
	}

	public void setDiaryManager(DiaryManager diaryManager) {
		this.diaryManager = diaryManager;
	}

	public void setMoodManager(MoodManager moodManager) {
		this.moodManager = moodManager;
	}
}
